<?php

namespace App\Http\Controllers\Steward;

use App\Models\Goods\GoodsMaster;
use App\Models\Goods\GoodsSale;
use App\Models\Goods\GoodsStore;
use App\Models\Goods\StewardGoodsDown;
use App\Models\Goods\StewardGoodsstoreDown;
use App\Models\Goods\WmsGoodsMaster;
use Faker\Provider\Uuid;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis as Redis;
use Maatwebsite\Excel\Facades\Excel;

class GoodsController extends Controller
{

    private $operation;         //商品列表操作
    private $enableSaleAmount = 0;  //商品列表可售库存

    /**
     * 商品列表
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index($id)
    {

        $data = [
            'id' => $id,
        ];

        return view('steward/goods/index', $data);
    }

    /**
     * 商品列表数据查询
     * @param Request $request
     * @param $id
     * @return array
     */
    public function search( Request $request, $id )
    {

        $mall_id = Redis::get('WDH_STEWARD_MALL_ID_' . session_id()) ? Redis::get('WDH_STEWARD_MALL_ID_' . session_id()) : 0;

        $where = [];

        //商家编码
        if($request->input('product_code')){
            $where[] = ['goods_master.product_code',$request->input('product_code')];
        }

        //商品条形码
        if($request->input('barcode')){
            $where[] = ['goods_sale.barcode',$request->input('barcode')];
        }

        //商品名称
        if($request->input('goods_name')){
            $where[] = ['goods_master.goodsName','like','%'.$request->input('goods_name').'%'];
        }

        //商品分类
        if($request->input('smallCategoryID')){
            $where[] = ['goods_master.smallCategoryID',$request->input('smallCategoryID')];
        }else{
            if($request->input('midCategoryID')){
                $where[] = ['goods_master.midCategoryID',$request->input('midCategoryID')];
            }else{
                if($request->input('bigCategoryID')){
                    $where[] = ['goods_master.bigCategoryID',$request->input('bigCategoryID')];
                }
            }
        }

        //商品价格
        if($request->input('price_min') && $request->input('price_max')){
            $where[] = ['goods_master.salePrice','>=',$request->input('price_min')];
            $where[] = ['goods_master.salePrice','<=',$request->input('price_max')];
        }

        if($id == 1){  //上架商品列表

            $goodsDatas = GoodsMaster::select('goods_master.mainPostID','goods_master.goodsName','goods_master.product_code','goods_marketing_strategy.salePrice','goods_sale.barcode')
				->rightJoin('wms_goods_master','goods_master.mainPostID','=','wms_goods_master.mainPostID')
                ->leftJoin('goods_sale','goods_master.product_code','=','goods_sale.product_code')
                ->leftJoin('goods_marketing_strategy','goods_master.product_code','=','goods_marketing_strategy.product_code')
                ->where('wms_goods_master.storeID',$mall_id)
                ->where($where)
                ->groupBy('goods_master.mainPostID')
                ->orderBy($request->input('sort'), $request->input('order'))
                ->paginate($request->input('limit'), ['*'], '', $request->input('offset') / 10 + 1 )
                ->toArray();

            //返回数组
            $result_data = [
                'total' => $goodsDatas['total'],
                'rows' =>[]
            ];

            $forsale = '已上架';

            if(!$goodsDatas){
                $result_data = [
                    'total' => 0,
                    'rows' =>[]
                ];
            }

            foreach ($goodsDatas['data'] as $goodsData){

                $this->enableSale($goodsData,1);

                if(!$goodsData['salePrice']){

                    $goods_sale = GoodsSale::where('product_code',$goodsData['product_code'])->first();
                    if($goods_sale){
                        $goods_sale = $goods_sale->toArray();
                        $price = $goods_sale['salePrice'];
                    }
                }else{
                    $price = $goodsData['salePrice'];
                }

                $result_data['rows'][] = [
                    'operation'    => $this->operation,
                    'forsale'      => $forsale,
                    'goodsName'    => $goodsData['goodsName'],
                    'product_code' => '商品编码：' . $goodsData['product_code'] . '<br/>条形码：' . $goodsData['barcode'],
                    'salePrice'    => '￥' . $price,
                    'enableSaleAmount' => $this->enableSaleAmount
                ];
            }
        }else{  //下架商品列表

            $goodsDatas = GoodsMaster::select('goods_master.mainPostID','goods_master.forsale','goods_master.goodsName','goods_master.product_code','goods_marketing_strategy.salePrice','goods_sale.barcode')
                ->rightJoin('steward_goods_down','goods_master.mainPostID','=','steward_goods_down.mainPostID')
				->leftJoin('goods_sale','goods_master.product_code','=','goods_sale.product_code')
                ->leftJoin('goods_marketing_strategy','goods_master.product_code','=','goods_marketing_strategy.product_code')
                ->where('steward_goods_down.storeID',$mall_id)
                ->where($where)
                ->groupBy('goods_master.mainPostID')
                ->orderBy($request->input('sort'), $request->input('order'))
                ->paginate($request->input('limit'), ['*'], '', $request->input('offset') / 10 + 1 )
                ->toArray();

            //返回数组
            $result_data = [
                'total' => $goodsDatas['total'],
                'rows' =>[]
            ];

            $forsale = '已下架';

            if(!$goodsDatas){
                $result_data = [
                    'total' => 0,
                    'rows' =>[]
                ];
            }

            foreach ($goodsDatas['data'] as $goodsData){

                $this->enableSale($goodsData,0);

                if(!$goodsData['salePrice']){

                    $goods_sale = GoodsSale::where('product_code',$goodsData['product_code'])->first();
                    if($goods_sale){
                        $goods_sale = $goods_sale->toArray();
                        $price = $goods_sale['salePrice'];
                    }
                }else{
                    $price = $goodsData['salePrice'];
                }

                $result_data['rows'][] = [
                    'operation'    => $this->operation,
                    'forsale'      => $forsale,
                    'goodsName'    => $goodsData['goodsName'],
                    'product_code' => '商品编码：' . $goodsData['product_code'] . '<br/>条形码：' . $goodsData['barcode'],
                    'salePrice'    => '￥' . $price,
                    'enableSaleAmount' => $this->enableSaleAmount
                ];
            }
        }

        return $result_data;
    }

    /**
     *可售库存查询
     */
    public function enableSale($goodsData,$type){

        $mall_id = Redis::get('WDH_STEWARD_MALL_ID_' . session_id()) ? Redis::get('WDH_STEWARD_MALL_ID_' . session_id()) : 0;

        //可售库存
        $wms_enableSaleAmount = GoodsStore::select('wms_goodsstore.enableSaleAmount as num','goods_sale.product_spec')
            ->leftJoin('goods_sale','wms_goodsstore.product_code','=','goods_sale.product_code')
            ->where('wms_goodsstore.mainPostID',$goodsData['mainPostID'])
            ->where('wms_goodsstore.storeID',$mall_id)
            ->get()->toArray();

        $steward_enableSaleAmount = StewardGoodsstoreDown::select('steward_goodsstore_down.enableSaleAmount as num','goods_sale.product_spec')
            ->leftJoin('goods_sale','steward_goodsstore_down.product_code','=','goods_sale.product_code')
            ->where('steward_goodsstore_down.mainPostID',$goodsData['mainPostID'])
            ->where('steward_goodsstore_down.storeID',$mall_id)
            ->get()->toArray();

        $count = count($wms_enableSaleAmount) + count($steward_enableSaleAmount);
        if(count($wms_enableSaleAmount)>1 ||count($steward_enableSaleAmount)>1 ||$count >1 ){
            $num = 0 ;
            if($wms_enableSaleAmount){
                foreach ($wms_enableSaleAmount as $v) {

                    if($v['product_spec'] == null){
                        $v['product_spec'] = 1 ;
                    }
                    $num += $v['num'] * $v['product_spec'];
                }
            }

            if($steward_enableSaleAmount){
                foreach ($steward_enableSaleAmount as $v) {

                    if($v['product_spec'] == null){
                        $v['product_spec'] = 1 ;
                    }
                    $num += $v['num'] * $v['product_spec'];
                }
            }

            $this->enableSaleAmount = $num;

            if($type == 1){

                $this->operation = '<a href="javascript:void(0)" class="forsale"  data_type="1" data_id="' . $goodsData['mainPostID'] . '">下架</a>&nbsp;&nbsp;<a href="javascript:void(0)" class="spec_type" data_id="' . $goodsData['mainPostID'] . '">规格管理</a>';
            }else{

                $this->operation = '<a href="javascript:void(0)" class="forsale"  data_type="0" data_id="' . $goodsData['mainPostID'] . '">上架</a>&nbsp;&nbsp;<a href="javascript:void(0)" class="spec_type" data_id="' . $goodsData['mainPostID'] . '">规格管理</a>';
            }
        }else{

            if($wms_enableSaleAmount){

                $this->enableSaleAmount = $wms_enableSaleAmount[0]['num'] * $wms_enableSaleAmount[0]['product_spec'];
            }

            if($steward_enableSaleAmount){

                $this->enableSaleAmount = $steward_enableSaleAmount[0]['num'] * $steward_enableSaleAmount[0]['product_spec'];
            }

            if($type == 1){

                $this->operation = '<a href="javascript:void(0)" class="forsale"  data_type="1" data_id="' . $goodsData['mainPostID'] . '">下架</a>';
            }else{

                $this->operation = '<a href="javascript:void(0)" class="forsale"  data_type="0" data_id="' . $goodsData['mainPostID'] . '">上架</a>';
            }
        }
    }

    /**
     * 主商品上下架操作
     */
    public function changeSaleMain($type,$ids){

        $mall_id = Redis::get('WDH_STEWARD_MALL_ID_' . session_id()) ? Redis::get('WDH_STEWARD_MALL_ID_' . session_id()) : 0;

        if($type == 1){ //下架
            foreach($ids as $id) {

                //主商品下架
                WmsGoodsMaster::where([['mainPostID', $id], ['storeID', $mall_id]])->delete();

                $steward_goods_data = new StewardGoodsDown();
                $steward_goods_data->uuid = Uuid::uuid();
                $steward_goods_data->timeStamp = date('Y-m-d h:i:s');
                $steward_goods_data->mainPostID = $id;
                $steward_goods_data->storeID = $mall_id;
                $steward_goods_data->creator = 'system';
                $steward_goods_data->createTime = date('Y-m-d h:i:s');
                $steward_goods_data->save();

                //库存表
                $spec_goods_datas = GoodsStore::where([['mainPostID', $id], ['storeID', $mall_id]])->get();

                if($spec_goods_datas){

                    $spec_goods_datas = $spec_goods_datas->toArray();
                    GoodsStore::where([['mainPostID', $id], ['storeID', $mall_id]])->delete();

                    foreach ($spec_goods_datas as $spec_goods_data) {

                        $steward_goodsstore_down = new StewardGoodsstoreDown();
                        $steward_goodsstore_down->uuid = Uuid::uuid();
                        $steward_goodsstore_down->timeStamp = date('Y-m-d h:i:s');
                        $steward_goodsstore_down->creator = $spec_goods_data['creator'];
                        $steward_goodsstore_down->createTime = date('Y-m-d h:i:s');
                        $steward_goodsstore_down->mainPostID = $spec_goods_data['mainPostID'];
                        $steward_goodsstore_down->storeID = $spec_goods_data['storeID'];
                        $steward_goodsstore_down->product_code = $spec_goods_data['product_code'];
                        $steward_goodsstore_down->enableSaleAmount = $spec_goods_data['enableSaleAmount'];
                        $steward_goodsstore_down->goodsName = $spec_goods_data['goodsName'];
                        $steward_goodsstore_down->lockAmount = $spec_goods_data['lockAmount'];
                        $steward_goodsstore_down->badAmount = $spec_goods_data['badAmount'];
                        $steward_goodsstore_down->virtualAmount = $spec_goods_data['virtualAmount'];
                        $steward_goodsstore_down->saleAmount = $spec_goods_data['saleAmount'];
                        $steward_goodsstore_down->lastAmount = $spec_goods_data['lastAmount'];
                        $steward_goodsstore_down->shelfID = $spec_goods_data['shelfID'];
                        $steward_goodsstore_down->sync_offline_stock = $spec_goods_data['sync_offline_stock'];
                        $steward_goodsstore_down->forsale = $spec_goods_data['forsale'];

                        $steward_goodsstore_down->save();
                    }
                }
            }
        }else{  //上架

            foreach($ids as $id) {

                //主商品上架
                StewardGoodsDown::where([['mainPostID', $id], ['storeID', $mall_id]])->delete();

                $wms_goods_data = new WmsGoodsMaster();
                $wms_goods_data->uuid = Uuid::uuid();
                $wms_goods_data->timeStamp = date('Y-m-d h:i:s');
                $wms_goods_data->mainPostID = $id;
                $wms_goods_data->storeID = $mall_id;
                $wms_goods_data->creator = 'system';
                $wms_goods_data->createTime = date('Y-m-d h:i:s');
                $wms_goods_data->save();

                //库存表
                $spec_goods_datas = StewardGoodsstoreDown::where([['mainPostID', $id], ['storeID', $mall_id]])->get();

                if($spec_goods_datas){

                    $spec_goods_datas = $spec_goods_datas->toArray();
                    StewardGoodsstoreDown::where([['mainPostID', $id], ['storeID', $mall_id]])->delete();

                    foreach ($spec_goods_datas as $spec_goods_data) {

                        $wmsgoodsstore = new GoodsStore();
                        $wmsgoodsstore->uuid = Uuid::uuid();
                        $wmsgoodsstore->timeStamp = date('Y-m-d h:i:s');
                        $wmsgoodsstore->creator = $spec_goods_data['creator'];
                        $wmsgoodsstore->createTime = date('Y-m-d h:i:s');
                        $wmsgoodsstore->mainPostID = $spec_goods_data['mainPostID'];
                        $wmsgoodsstore->storeID = $spec_goods_data['storeID'];
                        $wmsgoodsstore->product_code = $spec_goods_data['product_code'];
                        $wmsgoodsstore->goodsName = $spec_goods_data['goodsName'];
                        $wmsgoodsstore->enableSaleAmount = $spec_goods_data['enableSaleAmount'];
                        $wmsgoodsstore->lockAmount = $spec_goods_data['lockAmount'];
                        $wmsgoodsstore->badAmount = $spec_goods_data['badAmount'];
                        $wmsgoodsstore->virtualAmount = $spec_goods_data['virtualAmount'];
                        $wmsgoodsstore->saleAmount = $spec_goods_data['saleAmount'];
                        $wmsgoodsstore->lastAmount = $spec_goods_data['lastAmount'];
                        $wmsgoodsstore->shelfID = $spec_goods_data['shelfID'];
                        $wmsgoodsstore->sync_offline_stock = $spec_goods_data['sync_offline_stock'];
                        $wmsgoodsstore->forsale = $spec_goods_data['forsale'];

                        $wmsgoodsstore->save();
                    }
                }
            }
        }
    }

    /**
     * 商品状态修改
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function changeSale(Request $request )
    {

        $mall_id = Redis::get('WDH_STEWARD_MALL_ID_' . session_id()) ? Redis::get('WDH_STEWARD_MALL_ID_' . session_id()) : 0;

        $type = $request->input('type');
        $flag = $request->input('flag');
        $ids = $request->input('data');

        if($flag == 0){  //主商品操作上下架

            $this->changeSaleMain($type,$ids);

        }else{   //单品启用禁用操作

            $mainPostID = $request->input('mainPostID');

            if($type == 1){   //单品禁用
                $wms_goodsstore = GoodsStore::where([['product_code', $ids[0]], ['storeID', $mall_id]])->first();

                if($wms_goodsstore){

                    $wms_goodsstore = $wms_goodsstore->toArray();
                    GoodsStore::where([['product_code', $ids[0]], ['storeID', $mall_id]])->delete();

                    $steward_goodsstore_down = new StewardGoodsstoreDown();
                    $steward_goodsstore_down->uuid = Uuid::uuid();
                    $steward_goodsstore_down->timeStamp = date('Y-m-d h:i:s');
                    $steward_goodsstore_down->creator = $wms_goodsstore['creator'];
                    $steward_goodsstore_down->createTime = date('Y-m-d h:i:s');
                    $steward_goodsstore_down->mainPostID = $wms_goodsstore['mainPostID'];
                    $steward_goodsstore_down->storeID = $wms_goodsstore['storeID'];
                    $steward_goodsstore_down->product_code = $wms_goodsstore['product_code'];
                    $steward_goodsstore_down->goodsName = $wms_goodsstore['goodsName'];
                    $steward_goodsstore_down->enableSaleAmount = $wms_goodsstore['enableSaleAmount'];
                    $steward_goodsstore_down->lockAmount = $wms_goodsstore['lockAmount'];
                    $steward_goodsstore_down->badAmount = $wms_goodsstore['badAmount'];
                    $steward_goodsstore_down->virtualAmount = $wms_goodsstore['virtualAmount'];
                    $steward_goodsstore_down->saleAmount = $wms_goodsstore['saleAmount'];
                    $steward_goodsstore_down->lastAmount = $wms_goodsstore['lastAmount'];
                    $steward_goodsstore_down->shelfID = $wms_goodsstore['shelfID'];
                    $steward_goodsstore_down->sync_offline_stock = $wms_goodsstore['sync_offline_stock'];
                    $steward_goodsstore_down->forsale = $wms_goodsstore['forsale'];

                    $steward_goodsstore_down->save();
                }

                $count = GoodsStore::where([['mainPostID', $mainPostID], ['storeID', $mall_id]])->count();

                if(!$count){  //主商品下架

                    WmsGoodsMaster::where([['mainPostID', $mainPostID], ['storeID', $mall_id]])->delete();

                    $steward_goods_data = new StewardGoodsDown();
                    $steward_goods_data->uuid = Uuid::uuid();
                    $steward_goods_data->timeStamp = date('Y-m-d h:i:s');
                    $steward_goods_data->mainPostID = $mainPostID;
                    $steward_goods_data->storeID = $mall_id;
                    $steward_goods_data->creator = 'system';
                    $steward_goods_data->createTime = date('Y-m-d h:i:s');
                    $steward_goods_data->save();
                }

            }else{      //单品启用

                $steward_goodsstore_down = StewardGoodsstoreDown::where([['product_code', $ids[0]], ['storeID', $mall_id]])->first();

                if($steward_goodsstore_down){
                    $steward_goodsstore_down = $steward_goodsstore_down->toArray();
                    StewardGoodsstoreDown::where([['product_code', $ids[0]], ['storeID', $mall_id]])->delete();

                    $wms_goodsstore = new GoodsStore();
                    $wms_goodsstore->uuid = Uuid::uuid();
                    $wms_goodsstore->timeStamp = date('Y-m-d h:i:s');
                    $wms_goodsstore->creator = $steward_goodsstore_down['creator'];
                    $wms_goodsstore->createTime = date('Y-m-d h:i:s');
                    $wms_goodsstore->mainPostID = $steward_goodsstore_down['mainPostID'];
                    $wms_goodsstore->storeID = $steward_goodsstore_down['storeID'];
                    $wms_goodsstore->product_code = $steward_goodsstore_down['product_code'];
                    $wms_goodsstore->goodsName = $steward_goodsstore_down['goodsName'];
                    $wms_goodsstore->enableSaleAmount = $steward_goodsstore_down['enableSaleAmount'];
                    $wms_goodsstore->lockAmount = $steward_goodsstore_down['lockAmount'];
                    $wms_goodsstore->badAmount = $steward_goodsstore_down['badAmount'];
                    $wms_goodsstore->virtualAmount = $steward_goodsstore_down['virtualAmount'];
                    $wms_goodsstore->saleAmount = $steward_goodsstore_down['saleAmount'];
                    $wms_goodsstore->lastAmount = $steward_goodsstore_down['lastAmount'];
                    $wms_goodsstore->shelfID = $steward_goodsstore_down['shelfID'];
                    $wms_goodsstore->sync_offline_stock = $steward_goodsstore_down['sync_offline_stock'];
                    $wms_goodsstore->forsale = $steward_goodsstore_down['forsale'];

                    $wms_goodsstore->save();
                }

                $steward =  StewardGoodsDown::where([['mainPostID',$mainPostID],['storeID', $mall_id]])->get();

                if($steward){

                    //主商品上架
                    StewardGoodsDown::where([['mainPostID', $mainPostID], ['storeID', $mall_id]])->delete();

                    $wms_goods_data = new WmsGoodsMaster();
                    $wms_goods_data->uuid = Uuid::uuid();
                    $wms_goods_data->timeStamp = date('Y-m-d h:i:s');
                    $wms_goods_data->mainPostID = $mainPostID;
                    $wms_goods_data->storeID = $mall_id;
                    $wms_goods_data->creator = 'system';
                    $wms_goods_data->createTime = date('Y-m-d h:i:s');
                    $wms_goods_data->save();
                }
            }
        }

        return response()->json(['code' => 200 ,'message' => '操作成功']);
    }

    /**
     * 商品规格管理列表
     * @param Request $request
     * @param $mainPostID
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function specType(Request $request ,$mainPostID)
    {

        return view('steward/goods/specType', ['mainPostID' => $mainPostID,]);
    }

    /**
     * 商品规格列表查询
     * @param Request $request
     * @param $mainPostID
     * @return array
     */
    public function searchSpec(Request $request , $mainPostID)
    {

        $mall_id = Redis::get('WDH_STEWARD_MALL_ID_' . session_id()) ? Redis::get('WDH_STEWARD_MALL_ID_' . session_id()) : 0;

        $where = [];

        //商品编码
        if($request->input('product_code')){
            $where[] = ['goods_sale.product_code',$request->input('product_code')];
        }

        //返回数组
        $result_data = [
            'total' => 0,
            'rows' =>[]
        ];

        //启用商品表中单品数据
        $wms_goodsDatas = GoodsStore::select('goods_sale.goodsName','goods_sale.spec','goods_sale.product_code','goods_sale.barcode','goods_marketing_strategy.salePrice','goods_sale.product_spec','wms_goodsstore.enableSaleAmount')
                                ->leftJoin('goods_sale','wms_goodsstore.product_code','=','goods_sale.product_code')
                                ->leftJoin('goods_marketing_strategy','wms_goodsstore.product_code','=','goods_marketing_strategy.product_code')
                                ->where($where)
                                ->where('wms_goodsstore.mainPostID',$mainPostID)
                                ->where('wms_goodsstore.storeID',$mall_id)
                                ->orderBy($request->input('sort'), $request->input('order'))
                                ->paginate($request->input('limit'), ['*'], '', $request->input('offset') / 10 + 1 )
                                ->toArray();
        $wms_count = 0;

        if($wms_goodsDatas){

            $wms_count = $wms_goodsDatas['total'];

            foreach ($wms_goodsDatas['data'] as $wms_goodsData){

                if($wms_goodsData['product_spec'] == null){
                    $wms_goodsData['product_spec'] = 1 ;
                }

                if(!$wms_goodsData['salePrice']){

                    $goods_sale = GoodsSale::where('product_code',$wms_goodsData['product_code'])->first();
                    if($goods_sale){
                        $goods_sale = $goods_sale->toArray();
                        $price = $goods_sale['salePrice'];
                    }
                }else{
                    $price = $wms_goodsData['salePrice'];
                }

                $result_data['rows'][] = [
                    'operation' =>'<a href="javascript:void(0)" class="forsale" data_type="1" data_mainPostID="' . $mainPostID . '" data_id="'. $wms_goodsData['product_code'].'">禁用</a>',
                    'forsale' =>  '已启用' ,
                    'goodsName' => $wms_goodsData['goodsName'],
                    'spec' => $wms_goodsData['spec'],
                    'product_code' => '商品编码：' . $wms_goodsData['product_code'] . '<br/>条形码：' . $wms_goodsData['barcode'],
                    'salePrice' => $price,
                    'enableSaleAmount' => $wms_goodsData['enableSaleAmount'] * $wms_goodsData['product_spec']
                ];
            }
        }

        //禁用商品表中单品数据
        $steward_goodsDatas = StewardGoodsstoreDown::select('goods_sale.goodsName','goods_sale.spec','goods_sale.product_code','goods_sale.barcode','goods_marketing_strategy.salePrice','goods_sale.product_spec','steward_goodsstore_down.enableSaleAmount')
                                ->leftJoin('goods_sale','steward_goodsstore_down.product_code','=','goods_sale.product_code')
                                ->leftJoin('goods_marketing_strategy','steward_goodsstore_down.product_code','=','goods_marketing_strategy.product_code')
                                ->where($where)
                                ->where('steward_goodsstore_down.mainPostID',$mainPostID)
                                ->where('steward_goodsstore_down.storeID',$mall_id)
                                ->orderBy($request->input('sort'), $request->input('order'))
                                ->paginate($request->input('limit'), ['*'], '', $request->input('offset') / 10 + 1 )
                                ->toArray();
        $steward_count = 0;

        if($steward_goodsDatas){

            $steward_count = $steward_goodsDatas['total'];

            foreach ($steward_goodsDatas['data'] as $steward_goodsData){

                if($steward_goodsData['product_spec'] == null){
                    $steward_goodsData['product_spec'] = 1 ;
                }

                if(!$steward_goodsData['salePrice']){

                    $goods_sale = GoodsSale::where('product_code',$steward_goodsData['product_code'])->first();
                    if($goods_sale){
                        $goods_sale = $goods_sale->toArray();
                        $price = $goods_sale['salePrice'];
                    }
                }else{
                    $price = $steward_goodsData['salePrice'];
                }

                $result_data['rows'][] = [
                    'operation' =>'<a href="javascript:void(0)" class="forsale" data_type="0" data_mainPostID="' . $mainPostID . '" data_id="'. $steward_goodsData['product_code'].'">启用</a>',
                    'forsale' =>  '已禁用' ,
                    'goodsName' => $steward_goodsData['goodsName'],
                    'spec' => $steward_goodsData['spec'],
                    'product_code' => '商品编码：' . $steward_goodsData['product_code'] . '<br/>条形码：' . $steward_goodsData['barcode'],
                    'salePrice' => $price,
                    'enableSaleAmount' => $steward_goodsData['enableSaleAmount'] * $steward_goodsData['product_spec']
                ];
            }
        }

        $result_data['total'] = $wms_count + $steward_count;

        return $result_data;
    }


    /**
     * 商品批量操作页
     */
    public function batchShelf() {

        return view('steward/goods/batch/shelf');

    }


    /**
     * 上下架模板导出
     */
    public function export(){

        return response()->download(resource_path().'/goods_shelf.xls', '商品批量上下架模板.xls');
    }

    /**
     * 商品批量操作上下架
     */
    public  function changeSaleALL(Request $request){

        $m_id = Redis::get('WDH_STEWARD_MALL_ID_' . session_id()) ? Redis::get('WDH_STEWARD_MALL_ID_' . session_id()) : 0;

        $url = 'public'.$request->input('file_url');
        $type = $request->input('type');
        $count_success = 0;
        $count_fail = 0;

        $res = Excel::load($url,function($reader) use ($type,$m_id,&$count_success,&$count_fail){

            $data = $reader->getSheet();

            if($data){

                $data = $data->toArray();
                unset($data[0]);
            }

            foreach ($data as $item) {

                if( empty($item[0])){

                    $count_fail ++;
                    continue;
                }


                $p_id = (int)$item[0];

                if($type == 0){ //

                    //单品禁用
                    $wms_goodsstore = GoodsStore::where([['product_code', $p_id], ['storeID', $m_id]])->first();

                    if($wms_goodsstore){

                        $wms_goodsstore = $wms_goodsstore->toArray();
                        GoodsStore::where([['product_code', $p_id], ['storeID', $m_id]])->delete();

                        $steward_goodsstore_down = new StewardGoodsstoreDown();
                        $steward_goodsstore_down->uuid = Uuid::uuid();
                        $steward_goodsstore_down->timeStamp = date('Y-m-d h:i:s');
                        $steward_goodsstore_down->creator = $wms_goodsstore['creator'];
                        $steward_goodsstore_down->createTime = date('Y-m-d h:i:s');
                        $steward_goodsstore_down->mainPostID = $wms_goodsstore['mainPostID'];
                        $steward_goodsstore_down->storeID = $wms_goodsstore['storeID'];
                        $steward_goodsstore_down->product_code = $wms_goodsstore['product_code'];
                        $steward_goodsstore_down->goodsName = $wms_goodsstore['goodsName'];
                        $steward_goodsstore_down->enableSaleAmount = $wms_goodsstore['enableSaleAmount'];
                        $steward_goodsstore_down->lockAmount = $wms_goodsstore['lockAmount'];
                        $steward_goodsstore_down->badAmount = $wms_goodsstore['badAmount'];
                        $steward_goodsstore_down->virtualAmount = $wms_goodsstore['virtualAmount'];
                        $steward_goodsstore_down->saleAmount = $wms_goodsstore['saleAmount'];
                        $steward_goodsstore_down->lastAmount = $wms_goodsstore['lastAmount'];
                        $steward_goodsstore_down->shelfID = $wms_goodsstore['shelfID'];
                        $steward_goodsstore_down->sync_offline_stock = $wms_goodsstore['sync_offline_stock'];
                        $steward_goodsstore_down->forsale = $wms_goodsstore['forsale'];

                        $steward_goodsstore_down->save();

                        $count = GoodsStore::where([['mainPostID', $wms_goodsstore['mainPostID']], ['storeID', $m_id]])->count();

                        if(!$count){  //主商品下架

                            WmsGoodsMaster::where([['mainPostID', $wms_goodsstore['mainPostID']], ['storeID', $m_id]])->delete();

                            $steward_goods_data = new StewardGoodsDown();
                            $steward_goods_data->uuid = Uuid::uuid();
                            $steward_goods_data->timeStamp = date('Y-m-d h:i:s');
                            $steward_goods_data->mainPostID = $wms_goodsstore['mainPostID'];
                            $steward_goods_data->storeID = $m_id;
                            $steward_goods_data->creator = 'system';
                            $steward_goods_data->createTime = date('Y-m-d h:i:s');
                            $steward_goods_data->save();
                        }
                        $count_success+=1;
                    }else{
                        $count_fail+=1;
                    }
                }else{

                    $steward_goodsstore_down = StewardGoodsstoreDown::where([['product_code', $p_id], ['storeID', $m_id]])->first();

                    if($steward_goodsstore_down){

                        $steward_goodsstore_down = $steward_goodsstore_down->toArray();
                        StewardGoodsstoreDown::where([['product_code', $p_id], ['storeID', $m_id]])->delete();

                        $wms_goodsstore = new GoodsStore();
                        $wms_goodsstore->uuid = Uuid::uuid();
                        $wms_goodsstore->timeStamp = date('Y-m-d h:i:s');
                        $wms_goodsstore->creator = $steward_goodsstore_down['creator'];
                        $wms_goodsstore->createTime = date('Y-m-d h:i:s');
                        $wms_goodsstore->mainPostID = $steward_goodsstore_down['mainPostID'];
                        $wms_goodsstore->storeID = $steward_goodsstore_down['storeID'];
                        $wms_goodsstore->product_code = $steward_goodsstore_down['product_code'];
                        $wms_goodsstore->goodsName = $steward_goodsstore_down['goodsName'];
                        $wms_goodsstore->enableSaleAmount = $steward_goodsstore_down['enableSaleAmount'];
                        $wms_goodsstore->lockAmount = $steward_goodsstore_down['lockAmount'];
                        $wms_goodsstore->badAmount = $steward_goodsstore_down['badAmount'];
                        $wms_goodsstore->virtualAmount = $steward_goodsstore_down['virtualAmount'];
                        $wms_goodsstore->saleAmount = $steward_goodsstore_down['saleAmount'];
                        $wms_goodsstore->lastAmount = $steward_goodsstore_down['lastAmount'];
                        $wms_goodsstore->shelfID = $steward_goodsstore_down['shelfID'];
                        $wms_goodsstore->sync_offline_stock = $steward_goodsstore_down['sync_offline_stock'];
                        $wms_goodsstore->forsale = $steward_goodsstore_down['forsale'];

                        $wms_goodsstore->save();

                        $steward =  StewardGoodsDown::where([['mainPostID',$steward_goodsstore_down['mainPostID']],['storeID', $m_id]])->get();

                        if($steward){

                            //主商品上架
                            StewardGoodsDown::where([['mainPostID', $steward_goodsstore_down['mainPostID']], ['storeID', $m_id]])->delete();

                            $wms_goods_data = new WmsGoodsMaster();
                            $wms_goods_data->uuid = Uuid::uuid();
                            $wms_goods_data->timeStamp = date('Y-m-d h:i:s');
                            $wms_goods_data->mainPostID = $steward_goodsstore_down['mainPostID'];
                            $wms_goods_data->storeID = $m_id;
                            $wms_goods_data->creator = 'system';
                            $wms_goods_data->createTime = date('Y-m-d h:i:s');
                            $wms_goods_data->save();
                        }
                        $count_success++;
                    }else{
                        $count_fail++;
                    }
                }
            }
        });

        unlink(public_path().$request->input('file_url'));

        return response()->json(['code' => 200 ,'message' => $count_success.'个商品操作成功,'.$count_fail.'个商品操作失败']);
    }
}